package com.bdqn.bookshop.controller.mall;


import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.bdqn.bookshop.common.BookConstants;
import com.bdqn.bookshop.common.LoginPhoneConstants;
import com.bdqn.bookshop.entity.MallUser;
import com.bdqn.bookshop.service.MallUserService;
import com.bdqn.bookshop.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private MallUserService mallUserService;


    /**
     * 登录
     * @param user
     * @param session
     * @param kaptcha
     * @param response
     * @param request
     * @param auto
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public Result<MallUser> login(MallUser user, HttpSession session, String kaptcha, HttpServletResponse response,HttpServletRequest request,String auto){
        Result<MallUser> rs=null;
        try {
            //取出系统验证码
            String sysCaptcha = (String) session.getAttribute("mallVerifyCode");
            rs= mallUserService.LoginUsers(user,kaptcha,sysCaptcha);
            if (rs.isSuccess()){
                if ("on".equals(auto)){

                    //保存cookie 实现自动登陆
                    Cookie UserName=new Cookie("username",user.getUserName());
                    Cookie password=new Cookie("password",user.getPassword());
                    UserName.setMaxAge(30*30);
                    password.setMaxAge(30*30);
                    UserName.setPath(request.getContextPath()+"/");
                    password.setPath(request.getContextPath()+"/");
                    response.addCookie(UserName);
                    response.addCookie(password);




                }
                session.setAttribute(BookConstants.LOGIN_MALL_USER,rs.getData());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;

    }

    /**
     * 注册
     * @param mallUser
     * @param repassword
     * @return
     */
    @PostMapping("/registerUser")
    @ResponseBody
    public Result registerUser(MallUser mallUser,String  repassword){
        return  mallUserService.registerUser(mallUser,repassword);
    }



    //注销
    @GetMapping("/logout")
    public String loginOut(HttpSession session,HttpServletRequest request,HttpServletResponse response){
        Cookie username = new Cookie("username", "");
        Cookie password=new Cookie("password","");
         // 设置持久时间为0
        username.setMaxAge(0);
        password.setMaxAge(0);
        // 设置共享路径
        username.setPath(request.getContextPath()+"/");
        password.setPath(request.getContextPath()+"/");
        // 向客户端发送 Cookie
        response.addCookie(username);
        response.addCookie(password);
        session.removeAttribute("mallUser");
        return "redirect:/mall/index.html";
    }


    //修改密码
    @PostMapping("/updatePassWord")
    @ResponseBody
    public  Result updatePassWord(Integer id,String password,String newPassWord,String repassword){
        return  mallUserService.updatePassWord(id,password,newPassWord,repassword);
    }


    //修改个人信息
    @RequestMapping("/memberInfo")
    @ResponseBody
    public  Result updateInfo(Integer id,MallUser mallUser,HttpSession session){
       Result<MallUser> rs= mallUserService.updateInfo(id, mallUser);
        if (rs.isSuccess()){
            session.setAttribute(BookConstants.LOGIN_MALL_USER,rs.getData());
        }
        return  rs;
    }



    @PostMapping("/loginPhone")
    @ResponseBody
    public Object loginPhone(@Param("phone")String phone){
        return mallUserService.SmsVerification(phone);
    }



}
